package com.jshoperxms.controller.mall.backstage.setting;

import com.jshoperxms.Response;
import com.jshoperxms.components.upload.qiniu.QiNiuResourcesTools;
import com.jshoperxms.controller.mall.backstage.base.SearchModel;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.statickey.TagWords;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.storage.model.FileListing;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by sdywcd on 2017/10/21.
 * Des:查看和管理存储在七牛上的云文件
 */
@Controller
@RequestMapping("/mall/resources/qiniu")
public class SysNetImgFilesController {

    @Resource
    private QiNiuResourcesTools qiNiuResourcesTools;
    private Map<String,Object>responsedata=new HashMap<>();


    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    public ResponseEntity findByPage(SearchModel searchModel, @RequestParam("search[value]") String search, @RequestParam("order[0][column]") String ordervalue, @RequestParam("order[0][dir]") String ordertype){

        FileListing fileListing=qiNiuResourcesTools.getQiniuResouresFiles();
        processPageList(fileListing.items);
        return Response.success(responsedata);
    }



    public void processPageList(FileInfo[] items){
        List<Map<String,Object>>data=new ArrayList<>();
        for(FileInfo bean:items){
            Map<String,Object>cellMap=new HashMap<>();
            cellMap.put("id",bean.hash);
            cellMap.put("key",bean.key);
            cellMap.put("fsize",bean.fsize);
            cellMap.put("puttime", DateTools.formateLongDateToString(bean.putTime));
            data.add(cellMap);
        }
        responsedata.put(TagWords.DATA,data);
    }
}
